Skip to content

Never return nil for position conversions#1168

Merged
ahoppen merged 1 commit into
swiftlang:mainfrom
ahoppen:never-fail-position-conversions
Apr 12, 2024
Merged

Never return nil for position conversions#1168
ahoppen merged 1 commit into
swiftlang:mainfrom
ahoppen:never-fail-position-conversions

Conversation

@ahoppen

@ahoppen ahoppen commented Apr 5, 2024

Copy link
Copy Markdown
Member

Instead of returning nil to indicate that the position conversion failed, log a fault and perform a best-effort recovery.

I think this allows us to perform better recovery and also makes code calling these position conversions a lot simpler because it doesn’t need to make decisions about what to do if position conversions fail.

@ahoppen ahoppen requested a review from bnbarham April 5, 2024 21:04
@ahoppen ahoppen requested a review from benlangmuir as a code owner April 5, 2024 21:04
@ahoppen

ahoppen commented Apr 5, 2024

Copy link
Copy Markdown
Member Author

@swift-ci Please test

let requestInfo = try RequestInfo(request: requestString)

let lineTable = LineTable(requestInfo.fileContents)
if let offset = lineTable.utf8OffsetOf(line: line - 1, utf8Column: column - 1) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it's worth it, but maybe we should keep a nil returning variant? In this case in particular, it would make sense to display an error about invalid line/col (though... we're currently just ignoring it anyway).

Comment on lines +189 to +190
let start = snapshot.positionOf(zeroBasedLine: line - 1, utf8Column: column - 1)
let end = snapshot.positionOf(zeroBasedLine: endLine - 1, utf8Column: endColumn - 1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And in this case I really don't think it makes sense to do closest match (really anywhere we're making modifications), so... I'm more leaning towards having nil variants 😅

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to not have nil returning position conversion variants. The cases such a variant would catch are:

  • If we have some offset error in the line and the token to rename happens to be right at the beginning or end of the file so that the line kicks it out of the valid line range. But then rename in the middle of the file is already very broken so I doubt that it would help much
  • Same argument for column indices.

We already have so many position conversion functions and I don’t want to add even more. Actually, I think it’s more likely that we would end up calling the nil returning variant somewhere in the future where recovery would have been sensible. And, in practice I’ve never seen us hit position conversion to fail, and the code is easier to maintain if you don’t have to deal with failed conversions.

Instead of returning `nil` to indicate that the position conversion failed, log a fault and perform a best-effort recovery.

I think this allows us to perform better recovery and also makes code calling these position conversions a lot simpler because it doesn’t need to make decisions about what to do if position conversions fail.
@ahoppen ahoppen force-pushed the never-fail-position-conversions branch from ffc0c9d to 08f1595 Compare April 11, 2024 20:59
@ahoppen ahoppen enabled auto-merge April 11, 2024 20:59
@ahoppen

ahoppen commented Apr 11, 2024

Copy link
Copy Markdown
Member Author

@swift-ci Please test

@ahoppen

ahoppen commented Apr 11, 2024

Copy link
Copy Markdown
Member Author

@swift-ci Please test Linux

1 similar comment
@ahoppen

ahoppen commented Apr 12, 2024

Copy link
Copy Markdown
Member Author

@swift-ci Please test Linux

@ahoppen ahoppen merged commit b4eb1e2 into swiftlang:main Apr 12, 2024
@ahoppen ahoppen deleted the never-fail-position-conversions branch August 4, 2025 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants